package com.getpebble.android.notifications.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.os.Build.VERSION;
import android.text.TextUtils;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.common.core.util.PblPreferences.PrefKey;
import com.getpebble.android.common.model.PblAndroidAppModel;
import com.getpebble.android.common.model.PblAndroidAppModel.Record;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;

public class CoreNotifications {
    private static final ArrayList<String> CALENDAR_PACKAGES = new ArrayList(Arrays.asList(new String[]{"com.google.android.calendar", "com.google.calendar", "com.anydo.cal"}));
    public static final String[] EMAIL_PACKAGE_NAMES = new String[]{"com.google.android.gm", "com.android.email", "com.sec.android.email", "com.htc.android.mail", "com.lge.email", "com.Motorola.motoemail", "com.motorola.blur.email"};
    private static final ArrayList<String> GMAIL_PACKAGES = new ArrayList(Arrays.asList(new String[]{"com.google.android.gm"}));
    static final String[] NOTIFICATION_PACKAGE_NAMES = new String[]{"com.getpebble.android", "com.whatsapp", "com.facebook", "com.google.android.talk", "com.google.android.apps.babel", "com.instagram", "com.pandora", "com.snapchat", "com.skype", "kik.android", "com.twitter", "com.spotify", "com.ebay", "com.pinterest", "com.soundcloud", "co.vine", "com.tinder", "com.linkedin", "com.ubercab", "com.google.android.apps.googlevoice", "com.enflick.android.TextNow", "uk.co.bbc.android.sportdomestic", "com.hipchat", "com.sonyericsson.alarm", "com.sec.android.app.clockpackage", "com.htc.android.worldclock", "com.motorola.blur.alarmclock", "com.android.alarmclock", "com.android.deskclock"};
    private static final ArrayList<String> PHONE_PACKAGES = new ArrayList(Arrays.asList(new String[]{"com.google.android.dialer"}));
    static final String[] SMS_PACKAGE_NAMES = new String[]{"com.android.mms"};
    private static final String TAG = CoreNotifications.class.getSimpleName();

    public static boolean allowGmailImapNotifications() {
        return VERSION.SDK_INT < 18;
    }

    public static boolean areNativeGmailNotificationsEnabled(Context context) {
        if (context == null) {
            Trace.error(TAG, "areNativeGmailNotificationsEnabled(): context was null");
            return false;
        }
        PblPreferences preferences = new PblPreferences(context);
        for (Entry<String, ?> entry : preferences.getSharedPreferences().getAll().entrySet()) {
            if (((String) entry.getKey()).startsWith(preferences.prefKeyToString(PrefKey.GMAIL_ACCOUNT_ENABLED_PREFIX))) {
                try {
                    Boolean enabled = (Boolean) entry.getValue();
                    if (enabled != null) {
                        Trace.debug(TAG, "areNativeGmailNotificationsEnabled; enabled = " + enabled);
                        if (enabled.booleanValue()) {
                            return true;
                        }
                    } else {
                        continue;
                    }
                } catch (ClassCastException e) {
                    Trace.debug(TAG, (String) entry.getKey(), e);
                }
            }
        }
        return false;
    }

    public static void disableNativeGmailNotifications(Context context) {
        if (context == null) {
            Trace.error(TAG, "disableNativeGmailNotifications(): context was null");
            return;
        }
        PblPreferences preferences = new PblPreferences(context.getApplicationContext());
        for (Entry<String, ?> entry : preferences.getSharedPreferences().getAll().entrySet()) {
            if (((String) entry.getKey()).startsWith(preferences.prefKeyToString(PrefKey.GMAIL_ACCOUNT_ENABLED_PREFIX))) {
                try {
                    Boolean enabled = (Boolean) entry.getValue();
                    if (enabled != null && enabled.booleanValue()) {
                        Trace.debug(TAG, "Disabling native gmail notifications for " + ((String) entry.getKey()));
                        preferences.getSharedPreferences().edit().putBoolean((String) entry.getKey(), false).commit();
                    }
                } catch (ClassCastException e) {
                    Trace.debug(TAG, (String) entry.getKey(), e);
                }
            }
        }
    }

    private static boolean areNativeSMSNotificationsEnabled(Context context) {
        return new PblPreferences(context.getApplicationContext()).getBooleanData(PrefKey.SMS_NOTIFICATIONS, true);
    }

    private static boolean areNativePhoneNotificationsEnabled(Context context) {
        return new PblPreferences(context).getBooleanData(PrefKey.CALL_NOTIFICATIONS, true);
    }

    private static boolean areNativeCalendarNotificationsEnabled(Context context) {
        if (context != null) {
            return new PblPreferences(context).getBooleanData(PrefKey.CALENDAR_NOTIFICATIONS, true);
        }
        return true;
    }

    public static void disableNotificationsForSMSClients(Context context) {
        if (context == null) {
            Trace.error(TAG, "Failed to disable notifications for sms clients -- context was null!");
        } else if (areNativeSMSNotificationsEnabled(context)) {
            Iterator i$ = getAllSMSClients(context).iterator();
            while (i$.hasNext()) {
                String smsPackage = (String) i$.next();
                Record defaultRecord = PblAndroidAppModel.getAppRecord(smsPackage, context.getContentResolver());
                if (!("com.google.android.talk".equals(smsPackage) || defaultRecord == null || !defaultRecord.chosen)) {
                    Trace.debug(TAG, "Disabling notifications for " + smsPackage);
                    PblAndroidAppModel.setChosen(smsPackage, false, context.getContentResolver());
                }
            }
        }
    }

    public static void disableNotificationsForNativeApps(Context context) {
        if (context == null) {
            Trace.warning(TAG, "Dropping request to disable notifications for native apps due to null context.");
            return;
        }
        Iterator i$;
        if (allowGmailImapNotifications() && areNativeGmailNotificationsEnabled(context) && getPrefsForList(context, GMAIL_PACKAGES)) {
            Trace.debug(TAG, "gmail native notifications enabled; disabling gmail packages");
            i$ = GMAIL_PACKAGES.iterator();
            while (i$.hasNext()) {
                PblAndroidAppModel.setChosen((String) i$.next(), false, PebbleApplication.getAppContext().getContentResolver());
            }
        }
        if (!allowGmailImapNotifications() && areNativeGmailNotificationsEnabled(context)) {
            Trace.debug(TAG, "Disabling native gmail notifications for 4.3+ user");
            disableNativeGmailNotifications(context);
            PblAndroidAppModel.setChosen("com.google.android.gm", true, PebbleApplication.getAppContext().getContentResolver());
        }
        if (areNativePhoneNotificationsEnabled(context) && getPrefsForList(context, PHONE_PACKAGES)) {
            Trace.debug(TAG, "phone native notifications enabled; disabling phone packages");
            i$ = PHONE_PACKAGES.iterator();
            while (i$.hasNext()) {
                PblAndroidAppModel.setChosen((String) i$.next(), false, PebbleApplication.getAppContext().getContentResolver());
            }
        }
        disableNotificationsForSMSClients(context);
        if (areNativeCalendarNotificationsEnabled(context) && getPrefsForList(context, CALENDAR_PACKAGES)) {
            Trace.debug(TAG, "calendar native notifications enabled; disabling calendar packages");
            i$ = CALENDAR_PACKAGES.iterator();
            while (i$.hasNext()) {
                PblAndroidAppModel.setChosen((String) i$.next(), false, PebbleApplication.getAppContext().getContentResolver());
            }
        }
    }

    public static void updateAllowedStateForNativeApps(Context context) {
        if (context == null) {
            Trace.warning(TAG, "Dropping request to disallow notifications for native apps due to null context.");
            return;
        }
        boolean areNativeGmailNotificationsEnabled;
        if (allowGmailImapNotifications() && areNativeGmailNotificationsEnabled(context)) {
            areNativeGmailNotificationsEnabled = true;
        } else {
            areNativeGmailNotificationsEnabled = false;
        }
        Iterator i$ = GMAIL_PACKAGES.iterator();
        while (i$.hasNext()) {
            boolean z;
            String packageName = (String) i$.next();
            if (areNativeGmailNotificationsEnabled) {
                z = false;
            } else {
                z = true;
            }
            PblAndroidAppModel.setAllowed(packageName, z, PebbleApplication.getAppContext().getContentResolver());
        }
        boolean areNativePhoneNotificationsEnabled = areNativePhoneNotificationsEnabled(context);
        i$ = PHONE_PACKAGES.iterator();
        while (i$.hasNext()) {
            PblAndroidAppModel.setAllowed((String) i$.next(), !areNativePhoneNotificationsEnabled, PebbleApplication.getAppContext().getContentResolver());
        }
        updateAllowedStateForSMSApps(context);
        boolean areNativeCalendarNotificationsEnabled = areNativeCalendarNotificationsEnabled(context);
        i$ = CALENDAR_PACKAGES.iterator();
        while (i$.hasNext()) {
            PblAndroidAppModel.setAllowed((String) i$.next(), !areNativeCalendarNotificationsEnabled, PebbleApplication.getAppContext().getContentResolver());
        }
    }

    public static void updateAllowedStateForSMSApps(Context context) {
        if (context == null) {
            Trace.error(TAG, "Failed to update allowed state for sms apps -- context was null.");
            return;
        }
        boolean nativeSMSNotificationsEnabled = areNativeSMSNotificationsEnabled(context);
        Iterator i$ = getAllSMSClients(context).iterator();
        while (i$.hasNext()) {
            String smsClient = (String) i$.next();
            if (!smsClient.equals("com.google.android.talk")) {
                PblAndroidAppModel.setAllowed(smsClient, !nativeSMSNotificationsEnabled, PebbleApplication.getAppContext().getContentResolver());
            }
        }
    }

    private static boolean getPrefsForList(Context context, ArrayList<String> packageNames) {
        if (context == null) {
            Trace.error(TAG, "getPrefsForList: Context was null!");
            return false;
        }
        Iterator i$ = packageNames.iterator();
        while (i$.hasNext()) {
            Record record = PblAndroidAppModel.getAppRecord((String) i$.next(), PebbleApplication.getAppContext().getContentResolver());
            if (record != null && record.chosen) {
                return true;
            }
        }
        return false;
    }

    public static boolean allowSelectionForNotification(String packageName, Context context) {
        if (packageName == null) {
            Trace.warning(TAG, "packageName is null");
            return false;
        } else if (GMAIL_PACKAGES.contains(packageName)) {
            if (areNativeGmailNotificationsEnabled(context)) {
                return false;
            }
            return true;
        } else if (PHONE_PACKAGES.contains(packageName)) {
            if (areNativePhoneNotificationsEnabled(context)) {
                return false;
            }
            return true;
        } else if (!CALENDAR_PACKAGES.contains(packageName)) {
            HashSet<String> allSMSClients = getAllSMSClients(context);
            if (allSMSClients == null || !allSMSClients.contains(packageName) || packageName.equals("com.google.android.talk") || !areNativeSMSNotificationsEnabled(context)) {
                return true;
            }
            return false;
        } else if (areNativeCalendarNotificationsEnabled(context)) {
            return false;
        } else {
            return true;
        }
    }

    static boolean isNotificationApp(String packageName) {
        for (String notificationPackageName : NOTIFICATION_PACKAGE_NAMES) {
            if (packageName.contains(notificationPackageName)) {
                return true;
            }
        }
        return false;
    }

    static boolean isEmailApp(String packageName) {
        for (String emailPackageName : EMAIL_PACKAGE_NAMES) {
            if (packageName.contains(emailPackageName)) {
                return true;
            }
        }
        return false;
    }

    static boolean isSmsApp(String packageName) {
        for (String notificationPackageName : SMS_PACKAGE_NAMES) {
            if (packageName.contains(notificationPackageName)) {
                return true;
            }
        }
        return false;
    }

    public static boolean getDefaultToggleState(String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            return false;
        }
        if (isEmailApp(packageName) || isNotificationApp(packageName) || isSmsApp(packageName)) {
            return true;
        }
        return false;
    }

    public static HashSet<String> getAllSMSClients(Context context) {
        if (context == null) {
            Trace.error(TAG, "Failed to get all sms clients -- context was null");
            return null;
        }
        HashSet<String> smsClients = new HashSet();
        smsClients.add("com.android.mms");
        for (ResolveInfo resolveInfo : context.getPackageManager().queryBroadcastReceivers(new Intent("android.provider.Telephony.SMS_DELIVER"), 0)) {
            ActivityInfo activityInfo = resolveInfo.activityInfo;
            if (activityInfo != null && "android.permission.BROADCAST_SMS".equals(activityInfo.permission)) {
                smsClients.add(activityInfo.packageName);
            }
        }
        return smsClients;
    }
}
